<?php
declare (strict_types=1);

namespace app\school\model\school\admin;

use app\common\model\Attach;
use app\common\model\BaseModel;

class SchoolDomain extends BaseModel
{
    /**
     * 表名
     *
     * @var string
     */
    protected $name = 'school_domain';

    /**
     * 设置自动写入时间
     *
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间显示格式
     *
     * @var string
     */
    protected $dateFormat = 'Y-m-d H:i';

    /**
     * 定义表主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 类型转换
     *
     * @var []
     */
    protected $type = [
        'cname_status' => 'integer',
        'is_ssl'       => 'integer',
        'ssl_pem'      => 'integer',
        'ssl_key'      => 'integer',
        'status'       => 'integer',
    ];

    /**
     * 新增事件
     *
     * @static
     * @param \think\Model $agreement
     * @return mixed|void
     */
    public static function onBeforeInsert($agreement)
    {
        // 机构
        $agreement->mhm_id = $agreement->getAttr('mhm_id') ?: request()->mhm_id;
    }

    /**
     * 机构信息
     *
     * @return \think\model\relation\BelongsTo
     */
    public function school()
    {
        return $this->belongsTo(School::class, 'mhm_id')->bind(['school_title' => 'title']);
    }

    public function sslPem()
    {
        return $this->belongsTo(Attach::class, 'ssl_pem')->bind(['ssl_pem_fileurl' => 'fileurl', 'ssl_pem_filename' => 'filename']);
    }

    public function sslKey()
    {
        return $this->belongsTo(Attach::class, 'ssl_key')->bind(['ssl_key_fileurl' => 'fileurl', 'ssl_key_filename' => 'filename']);
    }
}